package photostock.model.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import photostock.model.Album;
import photostock.model.Photo;
import photostock.model.User;


public class UserTest {

	@Test
	public void initUser() {
		User user = new User("shared");
		assertTrue(user.exists() && user.isDirectory() && !user.create());
		assertTrue(user.initChildrenItems());
		assertTrue(((Album)user.getChildrenItems().elementAt(0)).initSharedItems());
		assertTrue(user.getChildrenItems().size() == 1);
		assertTrue(""+user.getChildrenItems().elementAt(0).getChildrenItems().size(), user.getChildrenItems().elementAt(0).getChildrenItems().size() == 4);
		assertTrue(user.getType().equals("visiteur"));
		assertFalse(user.acceptMail());
		assertEquals(user.getMailAdresse(), "");
		assertEquals(user.getProfil(), "");
		assertEquals(user.getWebPath(), user.getWebPath(), user.getParentItem().getWebPath() + user.getLogin());
		assertEquals(user.getHardPath(), user.getHardPath(), user.getParentItem().getHardPath() + user.getLogin());
		assertTrue(user.getParentItem().getWebPath(), user.getParentItem().getWebPath().equals("/Photostock/images/albums/"));
	}
	
	@Test
	public void initExistingUser() {

		User user = new User("nico");
		assertTrue (user.exists() && user.isDirectory() && !user.create());
		assertTrue(user.getLogin().equals("nico"));
		assertTrue(user.login("atreides"));
		assertTrue(user.initChildrenItems());
		assertEquals(user.getChildrenItems().size(), 1);
		assertTrue(user.isLogged());
		assertTrue(user.getType(), user.getType().equals("inscrit"));
		assertTrue(user.acceptMail());
		assertEquals(user.getMailAdresse(), "xleto@hotmail.com");
		assertEquals(user.getProfil(), "default");
		assertEquals(user.getWebPath(), user.getParentItem().getWebPath() + user.getLogin());
		assertEquals(user.getHardPath(), user.getParentItem().getHardPath() + user.getLogin());
		user.disconnect();
		assertFalse(user.isLogged());
	}

	@Test
	public void testModify() {
		User user = new User("nico");
		assertTrue (user.exists() && user.isDirectory() && !user.create());
		assertTrue(user.getLogin().equals("nico"));
		assertTrue(user.login("atreides"));
		assertTrue(user.isLogged());

		user.setType("admin");
		user.setProfil("");
		user.acceptMail(true);
		user.setMailAdresse("ncharpin@axxescaraibes.net");

		assertTrue(user.getType(), user.getType().equals("admin"));
		assertTrue(user.acceptMail());
		assertEquals(user.getMailAdresse(), "ncharpin@axxescaraibes.net");
		assertEquals(user.getProfil(), "");
		
		assertTrue(user.update());
		user.disconnect();
		assertFalse(user.isLogged());
		
		user = new User("nico");
		assertTrue(user.login("atreides"));
		assertTrue(user.isLogged());
		assertTrue(user.getType(), user.getType().equals("admin"));
		assertTrue(user.acceptMail());
		assertEquals(user.getMailAdresse(), "ncharpin@axxescaraibes.net");
		assertEquals(user.getProfil(), "");

		user.setType("inscrit");
		user.setProfil("default");
		user.acceptMail(true);
		user.setMailAdresse("xleto@hotmail.com");

		assertTrue(user.update());
		user.disconnect();
		assertFalse(user.isLogged());
	}
	
	
	@Test
	public void createUser() {
		
		User user = new User("kali");
		assertTrue (user.exists() && user.isDirectory() && !user.create());
		assertTrue(user.list().length == 0);
		assertTrue(user.delete());
		assertFalse(user.exists());
		assertFalse(user.login("atreides"));
		assertFalse(user.update());

		user = new User("kali");
		assertTrue (user.exists() && user.isDirectory() && !user.create());
		assertTrue(user.getLogin().equals("kali"));
		assertTrue(user.register("atreides", false, ""));
		assertTrue(user.isLogged());
		assertTrue(user.getType(), user.getType().equals("inscrit"));
		assertFalse(user.acceptMail());
		assertEquals(user.getMailAdresse(), "");
		assertEquals(user.getProfil(), "new");
		assertEquals(user.getWebPath(), user.getParentItem().getWebPath() + user.getLogin());
		assertEquals(user.getHardPath(), user.getParentItem().getHardPath() + user.getLogin());
		user.disconnect();
		assertFalse(user.isLogged());
	}
}
